<template>
  <div class="login">
    <span class="logo">
      <img src="@/assets/logo.png" style="width: 4.5rem;height: 4.5rem;"/></span>
    <p class="title">
      教师点名系统</p>
    <van-form validate-first>
    <div class="loginForm">
      <van-field v-model="form.mobile" placeholder="教师手机号" clearable :rules="[{ validator, message: '请输入正确的手机号' }]"></van-field>
    </div>
    <div class="btnClass">
      <van-button
        type="info"
        size="large"
        color="#89d3f8"
        loading-text="登录中..."
        :disabled="isLogin"
        native-type="submit"
        style="border-radius: 25px"
        >点名
      </van-button>
    </div>
    </van-form>
  </div>
</template>

<script>
import { userLogin } from '@/api/user'

export default {
  mounted() {},
  data() {
    return {
      form: {
        mobile: ''
      },
      isLogin: false // 是否登录
    }
  },
  computed: {
    baseURL() {
      return process.env.NODE_ENV === 'development' ? '/api' : process.env.VUE_APP_API
    }
  },
  methods: {
    async validator(val) {
      if (/^1[3456789]\d{9}$/.test(val)) {
        try {
          this.isLogin = true
          const data = await userLogin(this.form)
          localStorage.setItem('token', data.data.authorizationToken)
          this.$router.replace('/index').catch(err => {
            console.log('ok')
          })
          return true
        } catch (e) {
          this.$toast('登录失败')
        } finally {
          this.isLogin = false
        }
      }
      return false
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  width:100%;
  height:100%;
  position:fixed;
  background-color: #f5fafe;
  text-align: center;
  .title {
    font-size: 18px;
    font-family: PingFang SC;
    font-weight: bold;
    color: rgba(53, 53, 53, 1);
    text-align: left;
    margin-left: 10%;
  }

  .logo {
    text-align: center;
    line-height: 10px;
  }

  .btnClass{
    margin-right: 10%;
    margin-left: 10%;
    border-radius: 25px;
    margin-top: 25px;
  }

  .loginForm {
    background: white;
    border-radius: 30px;
    box-sizing: border-box;
    margin-right: 10%;
    margin-left: 10%;
  }
  .van-field{
    border-radius: 30px;
  }
}
</style>
